﻿using System.Collections.Generic;
using System.IO;
using System.Threading;
using Assets.Scripts.Example;
using UnityEngine;

public class MainEnter : MonoBehaviour
{
    private List<MainBtn> _btnList = new List<MainBtn>();

    private Example _example;
    public GameObject m_MainMenu;
    public UIGrid m_Grid;
    public UIPrefabPool m_BtnPrefabPool;
    public UIPrefabPool m_ExamplePrefabPool;
    public UIButton m_ResetBtn;
    public UIButton m_SnapShotWithoutGCBtn;
    public UIButton m_SnapShotBtn;

    private void Awake()
    {
        InitComponent();
        AddEvent();
    }

    private void Start()
    {
        LoadMainBtn();
        ShowMainMenu(true);
    }

    private void AddEvent()
    {
        EventDelegate.Set(m_ResetBtn.onClick, OnResetBtnClick);
        EventDelegate.Set(m_SnapShotWithoutGCBtn.onClick, OnSnapshotBtnWithoutGCClick);
        EventDelegate.Set(m_SnapShotBtn.onClick, OnSnapshotBtnWithoutGCClick);
    }

    private void OnResetBtnClick()
    {
        if (_example != null)
        {
            _example.OnRelease();
        }
        ShowMainMenu(true);
    }

    private void OnSnapshotBtnWithoutGCClick()
    {
        MonoProfiler.BeginSampleWithoutGC("[MainEnter][OnSnapshotBtnWithoutGCClick]");
    }

    private void OnSnapshotBtnClick()
    {
        MonoProfiler.BeginSample("[MainEnter][OnSnapshotBtnClick]");
    }

    private void ShowMainMenu(bool isShow)
    {
        NGUITools.SetActive(m_MainMenu, isShow);
        NGUITools.SetActive(m_ResetBtn.gameObject, !isShow);
        NGUITools.SetActive(m_SnapShotWithoutGCBtn.gameObject, !isShow);
        NGUITools.SetActive(m_SnapShotBtn.gameObject, !isShow);
        if (_example != null)
        {
            NGUITools.SetActive(_example.gameObject, !isShow);
        }
    }

    private void LoadMainBtn()
    {
        m_BtnPrefabPool.SetSpawnCount((int) EnumExample.end, ref _btnList);
        for (var i = 0; i < (int)EnumExample.end; i++)
        {
            _btnList[i].Init(i, OnMainBtnClick);
        }
        m_Grid.Reposition();
    }

    private void OnMainBtnClick(EnumExample exampleType)
    {
        LoadExample(exampleType);
        ShowMainMenu(false);
    }

    private void LoadExample(EnumExample exampleType)
    {
        if (_example == null)
        {
            _example = m_ExamplePrefabPool.SetSpawnCount<Example>();
        }
        else
        {
            _example.OnRelease();
        }

        _example.Init(exampleType);
    }


    private void InitComponent()
    {
        InitLogWrapper();
        var constUtil = new ConstUtil();
        constUtil.Init();
        MonoProfiler.Init();
        InitTaskManager();
        InitTimer();
    }

    private void InitLogWrapper()
    {
        LogWrapper.mainThreadId = Thread.CurrentThread.ManagedThreadId;
        LogWrapper.Init(Path.Combine(FileUtil.GetRootPath(EnumFileType.PERSISTENT), "Log"));
        LogWrapper.SetLogLevel(Consts.m_LogLevel);
    }

    private void InitTaskManager()
    {
        var taskManager = BehaviourUtil.AddMissingComponent<TaskManager>(gameObject);
        taskManager.Init();
    }

    public static MainEnter _instance;
    public static MainEnter Instance
    {
        get
        {
            if (_instance == null)
            {
                MainEnter[] enters = GameObject.FindObjectsOfType<MainEnter>();
                if (enters == null || enters.Length == 0)
                {
                    return null;
                }
                _instance = enters[0];
            }
            return _instance;
        }
    }

    private Timer _timer;
    private void InitTimer()
    {
        _timer = new Timer();
    }

    public static Timer GetTimer()
    {
        return Instance._timer;
    }
}